import { genCmptNo } from '@/utils/helper'
const AUDIO_BLOCKS_ADD_ITEM = 'AUDIO_BLOCKS_ADD_ITEM'
const AUDIO_BLOCKS_DEL_ITEM = 'AUDIO_BLOCKS_DEL_ITEM'
const AUDIO_BLOCKS_RESET = 'AUDIO_BLOCKS_RESET'

let I = 0
const state = {
  blocks: []
}

const getters = {
  audioBlocks (state) {
    let t = []
    state.blocks.forEach(n => {
      if (n.type !== 'audio') return
      t.push(n)
    })
    return t
  },
  blocks (state) {
    return state.blocks
  }
}

const actions = {
  addItem ({ commit, state, rootState }, item) {
    commit(AUDIO_BLOCKS_ADD_ITEM, item)
  },
  delItem ({ commit, state, rootState }, idx) {
    commit(AUDIO_BLOCKS_DEL_ITEM, idx)
  },
  reset ({ commit, state, rootState }) {
    commit(AUDIO_BLOCKS_RESET)
  },
}

const mutations = {
  AUDIO_BLOCKS_RESET (state, item) {
    state.blocks = []
  },
  AUDIO_BLOCKS_ADD_ITEM (state, item) {
    state.blocks.push(item)
  },
  AUDIO_BLOCKS_DEL_ITEM (state, idx) {
    let t = []
    state.blocks.forEach((n, i) => {
      if (i === idx) return
      t.push(n)
    })
    state.blocks = t
  },
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
